package com.wslds.base.dao;

import java.sql.SQLException;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.wslds.base.StatableBean;
import com.wslds.base.dao.page.PageAdapter;
import com.wslds.base.util.Collects;

public class BaseDaoImpl extends BaseDaoiBatis {

	public BaseDaoImpl(){}
	
	public <T> void save( String table, T t ){
		try {
			beforeSave(t);
			getSqlMapClient().insert(table+"_insert", t);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	public <T> void update( String table, T t ){
		try {
			beforeUpdate(t);
			getSqlMapClient().update(table+"_update", t);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	public <T> void delete( String table, T t ){
		try {
			getSqlMapClient().delete(table+"_delete", t);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	@SuppressWarnings("all")
	public <T> List<T> find( String table, T t ){
		try {
			List<T> list = getSqlMapClient().queryForList(table+"_select", t);
			if( Collects.isNull(list) ){
				return Collections.EMPTY_LIST;
			}else{
				return list;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	@SuppressWarnings("all")
	public <T> List<T> findExt( String sqlId, T t ){
		try {
			List<T> list = getSqlMapClient().queryForList(sqlId, t);
			if( Collects.isNull(list) ){
				return Collections.EMPTY_LIST;
			}else{
				return list;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	@SuppressWarnings("all")
	public <T> T findOne( String table, T t ){
		try {
			List<T> list = getSqlMapClient().queryForList(table+"_select", t);
			if( Collects.isNull(list) ){
				return null;
			}else{
				return list.get(0);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	@SuppressWarnings("all")
	public <T> T findOneExt( String sqlId, T t ){
		try {
			List<T> list = getSqlMapClient().queryForList(sqlId, t);
			if( Collects.isNull(list) ){
				return null;
			}else{
				return list.get(0);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	@SuppressWarnings("all")
	public <T> List<T> list( String table, T t, int offset, int limit ){
		try {
			List<T> list = getSqlMapClient().queryForList( table+"_select", t, offset, limit);
			if( Collects.isNull(list) ){
				return Collections.EMPTY_LIST;
			}else{
				return list;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	@SuppressWarnings("all")
	public <T> PageAdapter<T> page( String table, T t, int page , int limit ){
		try {
			List<T> list = getSqlMapClient().queryForList( table+"_select", t, (page-1)*limit, limit);
			
			Long maxSize = (Long)getSqlMapClient().queryForObject(table+"_select_count", t);
			
			if( Collects.isNull(list) ){
				return new PageAdapter(Collections.EMPTY_LIST,maxSize,page,limit);
			}else{
				return new PageAdapter(list,maxSize,page,limit);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	public <T> PageAdapter<T> page( String table, T t, int page  ){
		return page( table, t, page , 5);
	}
	@SuppressWarnings("all")
	public <T> PageAdapter<T> pageExt( String sqlId, T t, int page , int limit ){
		try {
			List<T> list = getSqlMapClient().queryForList( sqlId, t, (page-1)*limit, limit);
			
			Long maxSize = (Long)getSqlMapClient().queryForObject(sqlId+"_count", t);
			
			if( Collects.isNull(list) ){
				return new PageAdapter(Collections.EMPTY_LIST,maxSize,page,limit);
			}else{
				return new PageAdapter(list,maxSize,page,limit);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	public <T> PageAdapter<T> pageExt( String sqlId, T t, int page  ){
		return pageExt( sqlId, t, page , 5);
	}
	
	private void beforeSave(Object o) {
		if(o instanceof StatableBean) {
			StatableBean target = (StatableBean)o;
//			target.setStat(BlogCons.BLOG_STAT.MEI);
			target.setCreateDate(new Date());
			target.setModDate(new Date());
		}
	}
	
	private void beforeUpdate(Object o) {
		if(o instanceof StatableBean) {
			StatableBean target = (StatableBean)o;
			target.setModDate(new Date());
		}
	}
}
